/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.gdp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JToolBar;

import twaver.Element;
import twaver.ElementCallbackHandler;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.table.TElementTable;

public class Controlbar extends JToolBar{

	public Controlbar(final TElementTable table){
		final TDataBox box = table.getDataBox();
		
		this.addButton("view-refresh", "Refresh Color", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.iterator(new ElementCallbackHandler(){
					public boolean processElement(Element element) {
						element.putChartColor(TWaverUtil.getRandomColor());
						return true;
					}
				});
				table.repaint();
			}
		});
		this.addSeparator();
		this.addButton("list-add", "Add Selection", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.iteratorSelection(new ElementCallbackHandler(){
					public boolean processElement(Element element) {
						element.setVisible(true);
						return true;
					}
				});
			}
		});
		this.addButton("list-remove", "Remove Selection", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.iteratorSelection(new ElementCallbackHandler(){
					public boolean processElement(Element element) {
						element.setVisible(false);
						return true;
					}
				});
			}
		});
		this.addSeparator();
		this.addButton("go-top", "Move To Top", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.sortNoneColumn();
				box.moveSelectionToTop();
			}
		});
		this.addButton("go-up", "Move Up", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.sortNoneColumn();
				box.moveSelectionToUp();
			}
		});
		this.addButton("go-down", "Move Down", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.sortNoneColumn();
				box.moveSelectionToDown();
			}
		});
		this.addButton("go-bottom", "Move Bottom", new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.sortNoneColumn();
				box.moveSelectionToBottom();
			}
		});
	}
	
	private void addButton(String icon, String tooltipText, ActionListener l){
		JButton button = new JButton();
		button.addActionListener(l);
		button.setToolTipText(tooltipText);
		button.setMargin(TWaverConst.NONE_INSETS);
		button.setIcon(TWaverUtil.getIcon("/demo/chart/gdp/" + icon + ".png"));
		this.add(button);
	}
}